package com.weavernorth.salary;


import com.weavernorth.sysap.util.SyncUtil;
import com.weavernorth.util.Logger4jUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;

import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @program: ecology_erdos
 * @description: 定时调用V8数据, 插入建模表
 * @author: zm
 * @create: 2020-01-13
 **/


public class SalaryForV8Timer extends BaseCronJob {
//    public static Log log = LogFactory.getLog(SalaryForV8Timer. class.getName());
Logger log = Logger4jUtil.getLoggerByName("/action/api/salary/GetV8MessForParam/");
    @Override
    public void execute() {
        //执行存储过程类
        GetV8MessageUtil getV8MessageUtil = new GetV8MessageUtil();
        //获取员工信息类
        SyncUtil syncUtil = new SyncUtil();
        String strFormid = syncUtil.getModeIdBytableName("uf_xsyjhzb");
        //获取当前年月
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date()); //设置时间为当前时间
        cal.add(Calendar.MONTH, -1);
        String preMonth=format.format(cal.getTime());
        String year = preMonth.split("-")[0];
        String month =  preMonth.split("-")[1];
        log.info("当前年:" + year + ",月:" + month);
        //年月
        String strYearMonth=String.valueOf(year)+String.valueOf(month);
        //数据库操作
        RecordSet rs = new RecordSet();
        RecordSet rsCs = new RecordSet();
        //查询当前的店铺信息
        String sql = "select id,dpsapid ,dp from uf_dpxx";
        log.info("获取当前店铺信息sql:" + sql);
        rs.execute(sql);
        while (rs.next()) {
            //Sap店铺id
            String strDpSapid = Util.null2String(rs.getString("dpsapid"));
            //当前店铺id
            String strDpid = Util.null2String(rs.getString("dp"));
            try {
                log.info("获取店铺sap编号:" + strDpSapid);
                //执行存储过程,获取V8数据
                List<MessForV8Vo> listMess = getV8MessageUtil.getV8Mess(String.valueOf(year), String.valueOf(month), strDpSapid, strDpid, rsCs);
                 log.info("v8返回数据长度:"+(listMess.size()));
                log.info("v8返回数据:"+listMess.toString());
                //插入建模表
                if(listMess.size()>0){
                getV8MessageUtil.insertV8Mess(strFormid,strYearMonth, listMess, rsCs);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }


    }



    public void getV8(String year,String month,String strYearMonth) {
        //执行存储过程类
        GetV8MessageUtil getV8MessageUtil = new GetV8MessageUtil();
        //获取员工信息类
        SyncUtil syncUtil = new SyncUtil();
        String strFormid = syncUtil.getModeIdBytableName("uf_xsyjhzb");

        //数据库操作
        RecordSet rs = new RecordSet();
        RecordSet rsCs = new RecordSet();
        //查询当前的店铺信息
        String sql = "select id,dpsapid ,dp from uf_dpxx";
        log.info("获取当前店铺信息sql:" + sql);
        rs.execute(sql);
        while (rs.next()) {
            //Sap店铺id
            String strDpSapid = Util.null2String(rs.getString("dpsapid"));
            //当前店铺id
            String strDpid = Util.null2String(rs.getString("dp"));
            try {
                log.info("获取店铺sap编号:" + strDpSapid);
                //执行存储过程,获取V8数据
                List<MessForV8Vo> listMess = getV8MessageUtil.getV8Mess(String.valueOf(year), String.valueOf(month), strDpSapid, strDpid, rsCs);
                log.info("v8返回数据长度:"+(listMess.size()));
                //插入建模表
                if(listMess.size()>0){
                    getV8MessageUtil.insertV8Mess(strFormid,strYearMonth, listMess, rsCs);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }


    }

}




